#include "../header.h"

int main(int argc, char const *argv[])
{

    //1.socket
    int sid = socket(AF_INET,SOCK_DGRAM,0);
    printf("sid = %d\n",sid);
    //!----------------------广播属性设定代码部分---------------------//
    //2.修改属性
    int value = 0;
    int size = sizeof(value);
    getsockopt(sid,SOL_SOCKET,SO_BROADCAST,&value,&size);
    //设置属性
    value = 1;
    setsockopt(sid,SOL_SOCKET,SO_BROADCAST,&value,size);
    getsockopt(sid,SOL_SOCKET,SO_BROADCAST,&value,&size);
    printf("value = %d\n",value);
    //!--------------------------------------------------------//
    //3.绑定 bind
    struct sockaddr_in saddr = {0};
    //任意IP
    saddr.sin_addr.s_addr = INADDR_ANY;
    saddr.sin_family = AF_INET;
    //端口号
    saddr.sin_port = htons(atoi(argv[1]));
    if(bind(sid,(struct sockaddr*)&saddr,sizeof(saddr)) < 0 )
    {
        perror("bind error\n");
        return -2;
    }
    printf("绑定成功!\n");
    char bufs[128] = "";
    int len = 0 ;
    //4.读写
    while(1)
    {
        bzero(bufs,sizeof(bufs));
        len = recvfrom(sid,bufs,sizeof(bufs),0,NULL,NULL);
        printf("接收到数据是:%s len = %d\n",bufs,len);
        if(!strncmp(bufs,"quit",4))
        {
            printf("客户端退出...\n");
            break;
        }

    }
    //5.关闭
    close(sid);
    return 0;
}
